//
//  SecondScrollViewController.m
//  DesighBook
//
//  Created by 梅明 on 15/9/15.
//  Copyright (c) 2015年 梅明. All rights reserved.
//
#define CExpandContentOffset @"contentOffset"
#import "SecondScrollViewController.h"

#import "HttpRequestManager.h"
#import "UIImageView+AFNetworking.h"
#import "AuthorDetail.h"
#import "CExpandHeader.h"
#import "DesighModel.h"
#import "SecondScrollCellone.h"
#import "SecondScrollCelltwo.h"
#import "SecondScrollDetailViewController.h"

#define viewwith self.view.frame.size.width

@interface SecondScrollViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    CExpandHeader *_header;
    CGFloat _expandHeight;
    UIImageView *_expandView;
    NSMutableArray *_authorarray;
    UILabel *_detailLabel;
    CGFloat _cellheght1;
    int _cellheight;
    AuthorDetail *_authorMoel;
    
}

@property (weak, nonatomic) IBOutlet UITableView *myTableView;


@end

@implementation SecondScrollViewController
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        _authorarray = [[NSMutableArray alloc]init];
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated
{

    
}
- (IBAction)back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [_myTableView registerClass:[SecondScrollCellone class] forCellReuseIdentifier:@"firstcell"];
    [self loadDataFromNet];
    
}

-(void)loadDataFromNet
{
    [[HttpRequestManager shareInstnce]getSecondScrollviewInfoPageId:_numberid AndSuccess:^(id responseObjec) {
        NSDictionary *dic = responseObjec;
        _authorMoel = [[AuthorDetail alloc]init];
        _authorMoel.facepic = dic[@"memberInfo"][@"facePic"];
        _authorMoel.title = dic[@"name"];
        _authorMoel.authorname = dic[@"memberInfo"][@"nick"];
        _authorMoel.detailcontent = dic[@"comment"];
        _authorMoel.headimg = dic[@"imgUrl"];
        
        NSString *strTest = _authorMoel.detailcontent;
        UIFont *font = [UIFont fontWithName:nil size:16];
        CGSize size = CGSizeMake(viewwith,CGFLOAT_MAX);
        CGSize labelsize = [strTest sizeWithFont:font constrainedToSize:size lineBreakMode:_detailLabel.lineBreakMode];
        _cellheight = labelsize.height;

        
        NSArray *arr = dic[@"imgList"];
        for (NSDictionary *dic1 in arr) {
            DesighModel *model = [[DesighModel alloc]init];
            model.imgViewurl = dic1[@"imgUrl"];
            NSNumber *num = dic1[@"height"];
            model.height = num.intValue;
            num = dic1[@"width"];
            model.width = num.intValue;
            num = dic1[@"id"];
            model.numberid = num.stringValue;
            
            [_authorarray addObject:model];
        }
        
        [_myTableView reloadData];
        //创建自动放大图片
        _expandView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300)];
        [_expandView setImageWithURL:[NSURL URLWithString:_authorMoel.headimg]];
//        _header = [CExpandHeader expandWithScrollView:_myTableView expandView:_expandView];
        _header = [CExpandHeader expandWithScrollView:_myTableView expandView:_expandView Andcontent:_authorMoel.title];
        
    } AndFailure:^(NSError *error) {
        
        NSLog(@"error == %@",error);
        
    }];
    
  
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _authorarray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
        SecondScrollCelltwo *cell = [tableView dequeueReusableCellWithIdentifier:@"SecondScrollCelltwo"];
       [cell setimgWithModel:_authorarray Andindex:indexPath.row];
        cell.imgview.tag = 100 +indexPath.row;

     cell.block = ^(int i){
        NSNumber *num = @(i);
        [self performSegueWithIdentifier:@"detail22" sender:num];

        
    };
        return cell;
    
}

//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    
//    [self performSegueWithIdentifier:@"detail22" sender:indexPath];
//}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"detail22"]) {
        
        SecondScrollDetailViewController *view = segue.destinationViewController;
        NSNumber *num = sender;
        
        int i = num.intValue;
        view.index = i;
        DesighModel *model = _authorarray[i];
        view.numberid = model.numberid;
        view.array = _authorarray;
    }
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (_authorarray.count!=0) {
        
    

    //圆形白色背景
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, viewwith, 90+_cellheght1)];
    UIImageView *whiteImg = [[UIImageView alloc]initWithFrame:CGRectMake(viewwith/2-30, -30, 60, 60)];
    whiteImg.backgroundColor = [UIColor whiteColor];
    whiteImg.layer.cornerRadius = 30;
    [view addSubview:whiteImg];
    [view bringSubviewToFront:whiteImg];
    
    //作者头像
    UIImageView *faceimg = [[UIImageView alloc]initWithFrame:CGRectMake(viewwith/2-28, -28, 56, 56)];
    [faceimg setImageWithURL:[NSURL URLWithString:_authorMoel.facepic]];
    faceimg.layer.cornerRadius = 27;
    faceimg.clipsToBounds = YES;
    [view addSubview:faceimg];
    
    //作者用户名
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(viewwith/2-60, 28, 120, 30)];
    label1.text = _authorMoel.authorname;
    label1.textAlignment = NSTextAlignmentCenter;
    label1.textColor = [UIColor lightGrayColor];
    [view addSubview:label1];
    
    //白色横条
    UIView *horview = [[UIView alloc]initWithFrame:CGRectMake(viewwith/2-100, 58, 200, 0.3)];
    horview.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [view addSubview:horview];
    
        //详细内容
    _detailLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 65, 0, 0)];
    [_detailLabel setNumberOfLines:0];
    _detailLabel.lineBreakMode = NSLineBreakByWordWrapping;//换行方式
    NSString *strTest = _authorMoel.detailcontent;
    UIFont *font = [UIFont fontWithName:nil size:16];
    _detailLabel.font = font;
    _detailLabel.textAlignment = NSTextAlignmentLeft;
    CGSize size = CGSizeMake(viewwith,CGFLOAT_MAX);//LableWight标签宽度，固定的
    //计算实际frame大小，并将label的frame变成实际大小
    CGSize labelsize = [strTest sizeWithFont:font constrainedToSize:size lineBreakMode:_detailLabel.lineBreakMode];
    _detailLabel.frame = CGRectMake(10,70,labelsize.width-10, labelsize.height);
    _detailLabel.text = strTest;
    _detailLabel.textColor = [UIColor darkGrayColor];
    [view addSubview:_detailLabel];
        _cellheight = labelsize.height;
        view.backgroundColor = [UIColor whiteColor];
    return view;

    }
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 90+_cellheight;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    DesighModel *model = _authorarray[indexPath.row];
    return ((CGFloat)model.height/model.width) * (viewwith - 20);
}


@end
